package main

import (
	"flag"
	engine2 "gitee.com/jnshao/go-start/projects/crawler/engine"
	"gitee.com/jnshao/go-start/projects/crawler/scheduler"
	"gitee.com/jnshao/go-start/projects/crawler/zhenai/parser"
	"log"
	"net/rpc"
	"strings"

	ItemSaver "gitee.com/jnshao/go-start/projects/crawler_distributed/persist/client"
	"gitee.com/jnshao/go-start/projects/crawler_distributed/rpcsupport"
	Worker "gitee.com/jnshao/go-start/projects/crawler_distributed/worker/client"
)

const url = "http://www.zhenai.com/zhenghun"

// go run main.go --help
// itemsaver_host=":1234"
// worker_hosts=":8080,:9090"
var (
	itemSaverHost = flag.String("itemsaver_host", "", "itemsaver host")
	workerHosts   = flag.String("worker_hosts", "", "worker hosts (comma separated)")
)

func main() {
	flag.Parse() //不要忘记！

	c, err := ItemSaver.ItemSaver(*itemSaverHost)
	if err != nil {
		panic(err)
	}

	pool, err := createClientPool(strings.Split(*workerHosts, ","))
	if err != nil {
		panic(err)
	}
	processor := Worker.CreateProcess(pool)
	if err != nil {
		panic(err)
	}

	e := engine2.ConcurrentEngine{
		//接口的实现：指针接收者的实现只能以指针方式使用；值接收者都可
		//实现接口必须重写接口所有方法
		Scheduler:        &scheduler.QueueScheduler{},
		WorkerCount:      100,
		ItemChan:         c,
		RequestProcessor: processor,
	}
	f := engine2.NewFuncParser(parser.ParseCityList, "ParseCityList", nil)
	e.Run(engine2.Request{ //指针接收函数，必须必须先赋值给变量才能使用
		URL:    url,
		Parser: &f})
}

// 创建连接池
func createClientPool(hosts []string) (chan *rpc.Client, error) {
	var clients []*rpc.Client
	for _, host := range hosts {
		client, err := rpcsupport.NewClient(host)
		if err != nil {
			log.Printf("Error connecting to %s: %v", host, err)
			return nil, err
		}
		log.Printf("Connected to %s", host)
		clients = append(clients, client)
	}
	//轮流分发
	out := make(chan *rpc.Client)
	go func() {
		for {
			for _, client := range clients {
				out <- client
			}
		}
	}()
	return out, nil
}
